"""---------------------------
" Filename:     TestFunctions.py
" Description:  Holds test functions that interact with the C code
"
" Group 3:      nachOS
" Author(s):    Jan Durakiewicz
"
" Class:        CSE 325
" Instructor:   David Duggan
" Assignment:   Lab #1 Queue Manager
" Assigned:     January 22, 2013
" Due:          January 29, 2013
---------------------------"""
import ctypes
import QueueItems

"""
" Initializes the queue as described in the Test Interface assignment
" @author Jan Durakeiwcz
" @param ProcessQueue : a ctypes.CDLL library pointer to the ProcessQueue library
"""
def init_Q(ProcessQueue):
    print "Initializing queue"
    ProcessQueue.init()
    list(ProcessQueue)

"""
" Lists all items in the queue
" @param ProcessQueue : a ctypes.CDLL library pointer to the ProcessQueue library
"""
def list(ProcessQueue):
    #Grab the Queue object from the library
    QueueObj = QueueItems.Queue.in_dll(ProcessQueue, "process_queue")
   
    print "Queue Items : " + str(QueueObj.length)
    print "Free Space : " + str(20 - QueueObj.length)
    
    #as long as it's not empty, walk through it and print PIDs
    if(QueueObj.length > 0):
        
        QueueItemCurrent = QueueObj.head[0]
        print"--------------"        
        while True:
            CurrentPayload = ctypes.cast(QueueItemCurrent.payload, ctypes.POINTER(QueueItems.process_control_block))
            if QueueItemCurrent.next :
                print "Process Number : " + str( CurrentPayload.contents.pid)
                QueueItemCurrent = QueueItemCurrent.next[0]
                
            else :
                print "Process Number : " + str(CurrentPayload.contents.pid)             
                break

"""
" Enqueues a process on the process queue
" @author Jan Durakiewicz
" @param ProcessQueue : a ctypes.CDLL library pointer to the ProcessQueue library
" Rest of parameters are as defined in the Testing Interface document provided for the lab
"""
def enqueue(ProcessQueue, process_number, psw, page_table, reg0, reg1, reg2):
    regArr = (ctypes.c_int * 3)(reg0, reg1, reg2)
    if(ProcessQueue.enqueueProcess(process_number, psw, page_table, regArr) == -1):
        print "ENQUEUE UNSUCCESSFULL : queue full"
    else:
        print "ENQUEUE SUCCESS"

"""
" Dequeues a process from the queue
" @author Jan Durakiewicz
"""
def dequeue(ProcessQueue):
    pid = ProcessQueue.dequeueProcess()
    if pid == -1:
        print "DEQUEUE UNSUCCESSFUL : queue empty"
    else:
        print "DEQUEUED : " + str(pid)

"""
" Deletes a process with given PID
" @author Jan Durakiewicz
" @param ProcessQueue : a ctypes.CDLL library pointer to the ProcessQueue library
" @param process_number : as described in Testing Interface document in lab
"""
def delete(ProcessQueue, process_number):
    print "Deleted process " + str(ProcessQueue.deleteProcess(process_number))
